/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.web30.semantic.nntp;

import java.net.URISyntaxException;
import org.web30.semantic.core.model.MetaData;
import org.web30.semantic.core.model.ResourceAnalyzer;
import org.web30.semantic.core.model.ResourceRepository;
import org.web30.semantic.core.model.Web30Exception;
import java.net.URI;
import org.apache.james.mime4j.message.Message;
import org.web30.nntp.service.NNTPService;
import org.web30.nntp.service.NNTPService.NewsRecords;

/**
 *
 * @author Xu Tiejun
 */
public class ArticleRepository implements ResourceRepository {

    private ResourceAnalyzer analyzer = null;
    private String nntpServer = null;
    private int nntpPort = 119;
    private String authUser = null;
    private String authPass = null;
    /**
     * Group name of discussion
     */
    private String groupName = null;
    /**
     * URI of NNTP Server
     */
    private URI uri;
    /**
     * Service Handle
     */
    private NNTPService srv = null;
    /**
     * Records retrieved from NNTP Server
     */
    private NewsRecords records = null;
    private String lastId = null;

    public int getNNTPPort() {
        return nntpPort;
    }

    public void setNNTPPort(int nntpPort) {
        this.nntpPort = nntpPort;
    }

    public String getNNTPServer() {
        return nntpServer;
    }

    public void setNNTPServer(String nntpServer) {
        this.nntpServer = nntpServer;
    }
    /**
     * Constructor 
     * @param uri 
     * @param user
     * @param pwd
     */
    public ArticleRepository(String uri, String user, String pwd) {
        this.setURI(uri);
        this.authUser = user;
        this.authPass = pwd;
        this.srv = new NNTPService(
                this.nntpServer,
                this.nntpPort,
                this.authUser,
                this.authPass);
    }

    public String getAuthPass() {
        return authPass;
    }

    public void setAuthPass(String authPass) {
        this.authPass = authPass;
    }

    public String getAuthUser() {
        return authUser;
    }

    public void setAuthUser(String authUser) {
        this.authUser = authUser;
    }

    public String getLastId() {
        return lastId;
    }

    public void setLastId(String lastId) {
        this.lastId = lastId;
    }

    public void setResourceAnalyzer(ResourceAnalyzer parser) {
        this.analyzer = parser;
    }

    public boolean hasResource() {
        return this.records.hasNext();
    }

    public MetaData getNextResource() throws Web30Exception {
        Message msg = this.records.nextMime();
        this.setLastId(msg.getMessageId());
        MetaData res = this.analyzer.parseResource(msg);
        return res;
    }

    public void setURI(String uri) {
        try {
            this.uri = new URI(uri);
            this.nntpPort = this.uri.getPort();
            this.nntpServer = this.uri.getHost();
            this.groupName = this.uri.getPath().substring(this.uri.getPath().indexOf("/") + 1).trim();
        } catch (URISyntaxException ex) {
        }
    }

    public String getURI() {
        return this.uri.toASCIIString();
    }
    /**
     * Initialize the repository
     * @throws Web30Exception
     */
    public void initRepository() throws Web30Exception {
        try {
            this.srv.init();
            this.records = this.srv.getNewNews(this.groupName, this.lastId);
        } catch (Exception ex) {
            throw new Web30Exception(ex);
        }
    }
    /**
     * Close repository 
     */
    public void closeRepository() {        
        this.srv.close();
    }
}
